package com.xd.core.business.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xd.common.exception.XkdException;
import com.xd.core.business.entity.CarCredit;
import com.xd.core.business.service.ICarCreditService;
import com.xd.core.business.vo.CarCreditVo;
import com.xd.core.h5.vo.CarInsuranceDetail;
import com.xd.core.system.service.FileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 保单贷车辆信息 前端控制器
 * </p>
 *
 * @author ZX
 * @since 2025-06-19
 */
@Api(tags = "车辆信息")
@RestController
@RequestMapping("/business/car-credit")
public class CarCreditController {

    @Autowired
    private FileService fileService;
    @Autowired
    private ICarCreditService iCarCreditService;


    @ApiOperation("车辆信息列表")
    @GetMapping("/getLists/{incomingId}")
    public CarCreditVo getLists(@PathVariable Integer incomingId){
        List<CarCredit> carCredits = iCarCreditService.list(new LambdaQueryWrapper<CarCredit>().eq(CarCredit::getIncomingId, incomingId));
        // 计算总金额
        BigDecimal totalAmount = carCredits.stream()
                .map(carCredit -> carCredit.getAmount() == null ?BigDecimal.ZERO : carCredit.getAmount())
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        CarCreditVo carCreditVo = new CarCreditVo();
        carCreditVo.setCarCredits(carCredits);
        carCreditVo.setAmountTotal(totalAmount);
        return carCreditVo;
    }


    @ApiOperation("车辆信息详情")
    @GetMapping("/getCarCreditDetail/{id}")
    public CarCredit getCarCreditDetail(@PathVariable Integer id){
        return iCarCreditService.getById(id);
    }

    @Transactional(rollbackFor = XkdException.class)
    @ApiOperation("编辑车辆信息")
    @PostMapping("/updateCarCredit")
    public void updateCarCredit(@RequestBody CarCredit carCredit){
         iCarCreditService.updateById(carCredit);
        int count = iCarCreditService.count(new LambdaQueryWrapper<CarCredit>()
                .eq(CarCredit::getCarNo, carCredit.getCarNo())
                .eq(CarCredit::getIncomingId,carCredit.getIncomingId()));
        if (count > 1){
            throw new XkdException("车牌号已经录入,不能修改");
        }
    }

    @ApiOperation("新增车辆信息")
    @PostMapping("/insertCarCredit")
    public void insertCarCredit(@RequestBody CarCredit carCredit){
        int count = iCarCreditService.count(new LambdaQueryWrapper<CarCredit>()
                .eq(CarCredit::getCarNo, carCredit.getCarNo())
          .eq(CarCredit::getIncomingId,carCredit.getIncomingId()));
        if (count > 0){
            throw new XkdException("车牌号已经录入");
        }
        iCarCreditService.save(carCredit);
    }

    @ApiOperation("删除车辆信息")
    @DeleteMapping ("/deleteCarCredit/{id}")
    public void deleteCarCredit(@PathVariable Integer id){
        CarCredit carCredit1 = iCarCreditService.getById(id);
        String drivingUrl = carCredit1.getDrivingUrl();
        String insuranceUrl = carCredit1.getInsuranceUrl();
        if (StringUtils.isNotBlank(drivingUrl)){
            String[] split = drivingUrl.split(",");
            List<String> strings = Arrays.asList(split);
            fileService.filesRemove(strings);
        }
        if (StringUtils.isNotBlank(insuranceUrl)){
            String[] split = insuranceUrl.split(",");
            List<String> strings = Arrays.asList(split);
            fileService.filesRemove(strings);
        }
        iCarCreditService.removeById(id);
    }

    @ApiOperation("批量删除车辆信息")
    @DeleteMapping ("/deleteCarCredit")
    public void deleteCarCredit(@RequestParam List<Integer> ids){
        List<CarCredit> carCredits = iCarCreditService.list(new LambdaQueryWrapper<CarCredit>().in(CarCredit::getCarId, ids));
        carCredits.forEach(carCredit -> {
            String drivingUrl = carCredit.getDrivingUrl();
            String insuranceUrl = carCredit.getInsuranceUrl();
            if (StringUtils.isNotBlank(drivingUrl)){
                String[] split = drivingUrl.split(",");
                List<String> strings = Arrays.asList(split);
                fileService.filesRemove(strings);
            }
            if (StringUtils.isNotBlank(insuranceUrl)){
                String[] split = insuranceUrl.split(",");
                List<String> strings = Arrays.asList(split);
                fileService.filesRemove(strings);
            }
    });
    }



    @ApiOperation("后置服务费金额计算")
    @PostMapping("/commissionAmount")
    public BigDecimal commissionAmount(@RequestBody CarInsuranceDetail carInsuranceDetail){
        return iCarCreditService.commissionAmount(carInsuranceDetail);
    }

    }


